package com.rykj.widget.indicator;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rykj.R;

import java.util.List;

import androidx.viewpager.widget.ViewPager;

/**
 * 顶部Tab的导航区域 固定的顶部Indicator
 * @version 1.0
 */
public class ChangeablePagerIndicator extends LinearLayout {
    //默认的高亮文本颜色:白色
    public static final int COLOR_TEXT_HIGHLIGHT = Color.parseColor("#FFFFFFFF");
    //默认的正常文本颜色:灰色
    public static final int COLOR_TEXT_NORMAL = Color.parseColor("#77FFFFFF");
    //默认绘制图形的颜色:白色
    public static final int COLOR_GRAPHICS = Color.parseColor("#FFFFFFFF");
    // 默认Tab的文本大小 16sp
    public static final int TAB_TEXT_SIZE = 16;
    // 直线的高度 10dp
    public static final int LINE_HEIGHT = 10;
    // 最小的显示Tab的数量
    public static final int DEFAULT_VISIBLE_COUNTS = 3;
    // 绘制的类型:1.直线 2.三角形
    public static final int STYLE_LINE = 100;
    public static final int STYLE_TRIANGLE = 200;

    //三角形的比例
    public static final float TRIANGLE_RADIO = 1 / 6F;
    // 绘制三角形的画笔
    private Paint mTrianglePaint;
    // 绘制三角形的路径
    private Path mTrianglePath;
    // 三角形的宽
    private int mTriangleWidth;
    // 三角形的高
    private int mTriangleHeight;
    // 绘制直线的画笔
    private Paint mLinePaint;
    // 线的宽度
    private int mLineWidth;
    // 初始位置时三角形的位置
    private int mStartX;
    // 移动时三角形的位置
    private int mMoveX;
    // 绘制的类型
    private int mDrawStyle = STYLE_LINE;
    //默认显示的tab item数量
    private int mDefaultVisibleCounts;
    // 高亮文本的颜色
    private int mHighLightColor;
    //普通文本的颜色
    private int mNormalTextColor;
    // 文本大小
    private int mTextSize;
    //线的高度(strokeWidth)
    private int mLineHeight;
    // 直线或者三角形的颜色
    private int mGraphicsColor;
    /**
     * 设置ViewPager
     */
    private ViewPager mViewPager;
    /**
     * ViewPager监听
     */
    private IndicatorListener mListener = new IndicatorListener() {
        @Override
        public void selected(int position) { //设置高亮文本,当前ViewPager与Tab一一对应
            setHighLightText(position);
        }

        @Override
        public void scrollChanged(int position, float offset) { //进行Tab滚动
            scroll(position, offset);
        }
    };

    public ChangeablePagerIndicator(Context context) {
        this(context, null);
    }

    public ChangeablePagerIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
//获取自定义的值
        getCustomValue(context, attrs);
//初始化三角形的画笔
        initTrianglePaint();
//初始化直线的画笔
        initLinePaint();
    }

    /**
     * 获取自定义的值
     */
    private void getCustomValue(Context context, AttributeSet attrs) {
        TypedArray attributes = context
                .obtainStyledAttributes(attrs, R.styleable.ChangeablePagerIndicator);
        //获取默认显示的可见Tab的数量
        mDefaultVisibleCounts = attributes
                .getInt(R.styleable.ChangeablePagerIndicator_default_display_counts,
                        DEFAULT_VISIBLE_COUNTS);
        mDefaultVisibleCounts = Math.max(DEFAULT_VISIBLE_COUNTS, mDefaultVisibleCounts);
        //高亮文本的颜色
        mHighLightColor = attributes.getColor(R.styleable.ChangeablePagerIndicator_high_light_color,
                COLOR_TEXT_HIGHLIGHT);
        //普通文本的颜色
        mNormalTextColor = attributes
                .getColor(R.styleable.ChangeablePagerIndicator_normal_text_color,
                        COLOR_TEXT_NORMAL);
        //文本大小
        mTextSize = attributes
                .getInt(R.styleable.ChangeablePagerIndicator_text_size, TAB_TEXT_SIZE);
        //线的高度(strokeWidth)
        mLineHeight = attributes
                .getInt(R.styleable.ChangeablePagerIndicator_graphics_height, LINE_HEIGHT);
        //直线或者三角形的颜色
        mGraphicsColor = attributes
                .getColor(R.styleable.ChangeablePagerIndicator_graphics_color, COLOR_GRAPHICS);
        //绘制的类型
        boolean drawStyle = attributes
                .getBoolean(R.styleable.ChangeablePagerIndicator_is_draw_line, true);
        mDrawStyle = drawStyle?STYLE_LINE:STYLE_TRIANGLE;
        //直线的宽度
        mLineWidth = getScreenWidth() / mDefaultVisibleCounts;
        attributes.recycle();
    }

    /**
     * 初始化三角形的画笔
     */
    private void initTrianglePaint() {
        mTrianglePaint = new Paint();
        mTrianglePaint.setAntiAlias(true);
        //三角形的画笔颜色
        mTrianglePaint.setColor(mGraphicsColor);
        mTrianglePaint.setStyle(Paint.Style.FILL);
        //绘制角时的弧度
        mTrianglePaint.setPathEffect(new CornerPathEffect(2));
    }

    /**
     * 初始化直线的画笔
     */
    private void initLinePaint() {
        mLinePaint = new Paint(mTrianglePaint);
        mLinePaint.setStrokeWidth(mLineHeight);
    }

    /**
     * 获取屏幕的宽度
     */
    private int getScreenWidth() {
        WindowManager manager = (WindowManager) getContext()
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(metrics);
        return metrics.widthPixels;
    }

    @Override
    protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) {
        super.onSizeChanged(width, height, oldWidth, oldHeight);
        //创建三角形
        createIndicatorTriangle();
    }

    /**
     * 加载完XML时,调用此方法
     */
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        int coutns = getChildCount();
        if (coutns == 0) {
            return;
        }
        for (int i = 0; i < coutns; i++) {
            View view = getChildAt(i);
            LayoutParams params = (LayoutParams) view.getLayoutParams();
            params.weight = 0;
            params.width = getScreenWidth() / mDefaultVisibleCounts;
            view.setLayoutParams(params);
        }
        //设置监听
        setItemClickListener();
        //直线的宽度
        mLineWidth = getScreenWidth() / mDefaultVisibleCounts;
    }

    /**
     * Tab Item的点击监听
     */
    private void setItemClickListener() {
        for (int i = 0; i < getChildCount(); i++) { //点击的位置
            final int clickPosition = i;
            View view = getChildAt(i);
            if (view instanceof TextView) {
                view.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) { //ViewPager跳到对应的选中项
                        mViewPager.setCurrentItem(clickPosition);
                    }
                });
            }
        }
    }

    /**
     * 创建指示器的三角形
     */
    private void createIndicatorTriangle() {
        int tabWidth = getScreenWidth() / mDefaultVisibleCounts;
        //三角形的宽度
        mTriangleWidth = (int) (tabWidth * TRIANGLE_RADIO);
        //初始位置时三角形的位置
        mStartX = (tabWidth - mTriangleWidth) / 2;
        //三角形的高度
        mTriangleHeight = mTriangleWidth / 3; //绘制三三角形路径
        mTrianglePath = new Path();
        mTrianglePath.moveTo(0, 0);
        mTrianglePath.lineTo(mTriangleWidth, 0);
        mTrianglePath.lineTo(mTriangleWidth / 2, -mTriangleHeight);
        mTrianglePath.close();
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        canvas.save();
        if (mDrawStyle == STYLE_LINE) {
            //绘制直线
            canvas.drawLine(mMoveX, getHeight(), mMoveX + mLineWidth, getHeight(), mLinePaint);
        }
        if (mDrawStyle == STYLE_TRIANGLE) {
            //绘制三角形
            canvas.translate(mStartX + mMoveX, getHeight());
            canvas.drawPath(mTrianglePath, mTrianglePaint);
        }
        canvas.restore();
        super.dispatchDraw(canvas);
    }

    /**
     * 水平滚动
     */
    public void scroll(int position, float offset) {
        int tabWidth = getWidth() / mDefaultVisibleCounts; //指示器移动的距离
        mMoveX = (int) (tabWidth * (position + offset)); //移动Tab
        if (position >= (mDefaultVisibleCounts - 2) && offset > 0 && getChildCount() > mDefaultVisibleCounts && position != getChildCount() - 2) {
            if (mDefaultVisibleCounts != 1) {
                int scrollX = (position - (mDefaultVisibleCounts - 2)) * tabWidth + (int) (tabWidth * offset);
                this.scrollTo(scrollX, 0);
            } else {
                this.scrollTo((position * tabWidth + (int) (tabWidth * offset)), 0);
            }
        } //重新绘制
        invalidate();
    }

    /**
     * 设置绘制的类型
     * <p>
     * 1.STYLE_LINE 直线 2.STYLE_TRIANGLE 三角形
     */
    public void setDrawStyle(int style) {
        mDrawStyle = style;
        invalidate();
    }

    public void setDefaultVisibleCounts(int counts) {
        mDefaultVisibleCounts = counts;
        if (counts < DEFAULT_VISIBLE_COUNTS) {
            mDefaultVisibleCounts = DEFAULT_VISIBLE_COUNTS; //直线的宽度
        }
        mLineWidth = getScreenWidth() / mDefaultVisibleCounts;
    }

    /**
     * 不用加载布局文件,代码生成
     */
    public void setTabItems(List<String> titles) {
        if (titles == null) {
            return; //首先移除所有的View
        }
        this.removeAllViews(); //根据标题生成TextView
        for (String title : titles) {
            this.addView(generateTitleView(title));
        } //设置监听
        setItemClickListener();
    }

    /**
     * 根据标题生成TextView
     */
    private View generateTitleView(String title) {
        TextView tv = new TextView(getContext());
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
        params.width = getScreenWidth() / mDefaultVisibleCounts;
        tv.setGravity(Gravity.CENTER);
        tv.setText(title);
        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, mTextSize);
        tv.setTextColor(mNormalTextColor);
        tv.setLayoutParams(params);
        return tv;
    }

    public void setViewPager(ViewPager viewPager, int currentPos) {
        mViewPager = viewPager;
        viewPager.addOnPageChangeListener(mListener); //设置当前选中项
        viewPager.setCurrentItem(currentPos);
    }

    /**
     * 设置高亮显示的文本 也就是当前选中的文本
     */
    private void setHighLightText(int position) {
        for (int i = 0; i < getChildCount(); i++) {
            View view = getChildAt(i);
            if (view instanceof TextView) { //高亮颜色
                if (i == position) {
                    ((TextView) view).setTextColor(mHighLightColor); //正常颜色
                } else {
                    ((TextView) view).setTextColor(mNormalTextColor);
                }
            }
        }
    }
}
